package com.qiaoyanqing.ximalaya.discovermodule.discover_fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.qiaoyanqing.ximalaya.R;
import com.qiaoyanqing.ximalaya.discovermodule.discover_callback.DiscoverAlbumdetailService;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.DiscoverAlbumDetailEntity;
import com.qiaoyanqing.ximalaya.url.Urlconfig;
import com.qiaoyanqing.ximalaya.util.CircleTransform;
import com.qiaoyanqing.ximalaya.util.MyLog;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class AlbumDetalFragment extends Fragment implements Callback<DiscoverAlbumDetailEntity> {



    public AlbumDetalFragment() {
        // Required empty public constructor
    }

    private TextView intro,albumanchor,personalSignature,followers,deatilrecommendTitle1,deatilrecommendTitle2,deatilrecommendTitle3;
    private TextView deatilrecommendsubtitle1,deatilrecommendsubtitle2,deatilrecommendsubtitle3,detailnumber1,detailnumber2,detailnumber3;
    private TextView tracks1,tracks2,tracks3;

    private ImageView Usericon,deatilrecommendimage1,deatilrecommendimage2,deatilrecommendimage3;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_album_detal, container, false);

        initView(view);

        //加载网络数据
        initData();



        return view;

    }

    private void initView(View v) {
        //图片
        deatilrecommendimage1= (ImageView) v.findViewById(R.id.deatilrecommendimage1);
        deatilrecommendimage2= (ImageView) v.findViewById(R.id.deatilrecommendimage2);
        deatilrecommendimage3= (ImageView) v.findViewById(R.id.deatilrecommendimage3);
        Usericon= (ImageView) v.findViewById(R.id.usericon);
        //文字
        intro= (TextView) v.findViewById(R.id.intro);
        albumanchor= (TextView) v.findViewById(R.id.albumanchor);
        personalSignature= (TextView) v.findViewById(R.id.personalSignature);
        followers= (TextView) v.findViewById(R.id.followers);
        deatilrecommendTitle1= (TextView) v.findViewById(R.id.deatilrecommendTitle1);
        deatilrecommendTitle2= (TextView) v.findViewById(R.id.deatilrecommendTitle2);
        deatilrecommendTitle3= (TextView) v.findViewById(R.id.deatilrecommendTitle3);
        deatilrecommendsubtitle1= (TextView) v.findViewById(R.id.deatilrecommendsubtitle1);
        deatilrecommendsubtitle2= (TextView) v.findViewById(R.id.deatilrecommendsubtitle2);
        deatilrecommendsubtitle3= (TextView) v.findViewById(R.id.deatilrecommendsubtitle3);

        detailnumber1= (TextView) v.findViewById(R.id.detailnumber1);
        detailnumber2= (TextView) v.findViewById(R.id.detailnumber2);
        detailnumber3= (TextView) v.findViewById(R.id.detailnumber3);
        tracks1= (TextView) v.findViewById(R.id.tracks1);
        tracks2= (TextView) v.findViewById(R.id.tracks2);
        tracks3= (TextView) v.findViewById(R.id.tracks3);




    }

    private void initData() {

        long album =getActivity().getIntent().getIntExtra("album",398585);
        MyLog.i("flag","..."+album);

        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(Urlconfig.ALBUMDETAIL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        DiscoverAlbumdetailService albumdetailService=retrofit.create(DiscoverAlbumdetailService.class);

        albumdetailService.get(album).enqueue(this);

    }


    @Override
    public void onResponse(Call<DiscoverAlbumDetailEntity> call, Response<DiscoverAlbumDetailEntity> response) {



        MyLog.i("flag","responsedetail"+response.body().getData().getUser().getNickname());



        intro.setText(response.body().getData().getDetail().getIntro());
        albumanchor.setText(response.body().getData().getUser().getNickname());

        personalSignature.setText(response.body().getData().getUser().getPersonalSignature());
        followers.setText( "已被"+(int)(response.body().getData().getUser().getFollowers()/10000)+"万人关注");
        deatilrecommendTitle1.setText(response.body().getData().getRecs().getList().get(0).getTitle());
        deatilrecommendTitle2.setText(response.body().getData().getRecs().getList().get(1).getTitle());
        deatilrecommendTitle3.setText(response.body().getData().getRecs().getList().get(2).getTitle());
        deatilrecommendsubtitle1.setText(response.body().getData().getRecs().getList().get(0).getIntro());
        deatilrecommendsubtitle2.setText(response.body().getData().getRecs().getList().get(1).getIntro());
        deatilrecommendsubtitle3.setText(response.body().getData().getRecs().getList().get(2).getIntro());

        detailnumber1.setText((int) response.body().getData().getRecs().getList().get(0).getPlayTimes()/10000+"万");
        detailnumber2.setText((int) response.body().getData().getRecs().getList().get(1).getPlayTimes()/10000+"万");
        detailnumber3.setText((int) response.body().getData().getRecs().getList().get(2).getPlayTimes()/10000+"万");
        tracks1.setText(response.body().getData().getRecs().getList().get(0).getTracks()+"集");
        tracks2.setText(response.body().getData().getRecs().getList().get(1).getTracks()+"集");
        tracks3.setText(response.body().getData().getRecs().getList().get(2).getTracks()+"集");
        //
        Picasso.with(getContext()).load(response.body().getData().getRecs().getList().get(0).getCoverSmall()).into(deatilrecommendimage1);
        Picasso.with(getContext()).load(response.body().getData().getRecs().getList().get(1).getCoverSmall()).into(deatilrecommendimage2);
        Picasso.with(getContext()).load(response.body().getData().getRecs().getList().get(2).getCoverSmall()).into(deatilrecommendimage3);
        Picasso.with(getContext()).load(response.body().getData().getUser().getSmallLogo()).transform(new CircleTransform()).into(Usericon);
        MyLog.i("flag","-------usericon"+response.body().getData().getUser().getSmallLogo().toString());

    }

    @Override
    public void onFailure(Call<DiscoverAlbumDetailEntity> call, Throwable t) {

    }
}
